package org.szgb.console.ht.electricity.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.ht.electricity.entity.HtElectricitySettlementBillItem;
import org.szgb.console.ht.electricity.mapper.HtElectricitySettlementBillItemMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtElectricitySettlementBillItemService {
    private final HtElectricitySettlementBillItemMapper htElectricitySettlementBillItemMapper;

    @Autowired
    public HtElectricitySettlementBillItemService(HtElectricitySettlementBillItemMapper htElectricitySettlementBillItemMapper){
        this.htElectricitySettlementBillItemMapper = htElectricitySettlementBillItemMapper;
    }

    @Transactional(readOnly = true)
    public HtElectricitySettlementBillItem get(String id) {
        return htElectricitySettlementBillItemMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtElectricitySettlementBillItem> findList(HtElectricitySettlementBillItem htElectricitySettlementBillItem) {
        if (htElectricitySettlementBillItem != null && htElectricitySettlementBillItem.getPageDto() != null) {
            Page<HtElectricitySettlementBillItem> page = new Page<>(htElectricitySettlementBillItem.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htElectricitySettlementBillItem.setPage(page);
        }
        return htElectricitySettlementBillItemMapper.findList(htElectricitySettlementBillItem);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtElectricitySettlementBillItem htElectricitySettlementBillItem) {
        return htElectricitySettlementBillItemMapper.getCount(htElectricitySettlementBillItem);
    }

    @Transactional()
    public void save(HtElectricitySettlementBillItem htElectricitySettlementBillItem) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htElectricitySettlementBillItem.getId() == null || htElectricitySettlementBillItem.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htElectricitySettlementBillItem.setCreatedBy(htElectricitySettlementBillItem.getUpdatedBy());
            // 添加UUID
            htElectricitySettlementBillItem.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htElectricitySettlementBillItemMapper.insert(htElectricitySettlementBillItem);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htElectricitySettlementBillItemMapper.update(htElectricitySettlementBillItem);
        }
    }

    @Transactional
    public void delete(String ids) {
        htElectricitySettlementBillItemMapper.delete(ids.split(","));
    }
}
